<!--
 * @Author: 
 * @Date: 2021-12-27 20:53:07
 * @LastEditors: Joe Yan
 * @LastEditTime: 2022-01-03 11:45:32
 * @Description: file content
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>  
    <script>

        /**
         * 1、根据call的规则设置上下文的对象，也即是this的指向
         * 2、通过设置context的属性，将this绑定到context
         * 3、执行绑定在context上的函数，并传递参数
         * 4. 删除临时变量， 并返回结果
        */

        Function.prototype.myCall = function(context, ...arr) {
            if (context == null || context == undefined) {
                context = window
            } else {
                context = Object(context);
            }

            const sy = Symbol("特殊变量");
            context[sy] = this;
            const result = context[sy](...arr);
            // 删除context上的属性
            delete context[sy];
            return result;
        }

        function foo(i,m,n) {
            console.log(this, i+m+n);
        }

        var obj = {name: "Ming"}
        foo(7,8,8);
        foo.myCall(obj, 8,8,8)
    </script>
</body>
</html>
